﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace UmdhTracer
{
    
    public partial class UmdhTracerSettings : Form
    {
        public Settings SettingsEdited;
        public UmdhTracerSettings(Settings settings)
        {
            SettingsEdited = settings.Clone();
            InitializeComponent();
        }

        private void OkButtonClick(object sender, EventArgs e)
        {
            if (File.Exists(SettingsEdited.UmdhPath) == false)
            {
                MessageBox.Show("UMDH not found, please check again the settings.");
            }
            else
            {
                DialogResult = DialogResult.OK;
            }
        }

        private void CancelButtonClick(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void UmdhTracerSettings_Load(object sender, EventArgs e)
        {
            propertyGrid.SelectedObject = SettingsEdited;
        }

    }

    public class ExeFileNameEditor : System.Windows.Forms.Design.FileNameEditor
    {
        // Enforce fw extension type
        protected override void InitializeDialog(OpenFileDialog openFileDialog)
        {
            base.InitializeDialog(openFileDialog);
            openFileDialog.Filter = "UMDH Executable|umdh.exe";
        }
    }

    [XmlRoot("Settings")]
    public class Settings
    {
        [CategoryAttribute("UMDH Tracer Settings Form"), DescriptionAttribute("Path to UMDH executable")]
        [EditorAttribute(typeof(ExeFileNameEditor), typeof(System.Drawing.Design.UITypeEditor))]
        public string UmdhPath { get; set; }

        [CategoryAttribute("UMDH Tracer Settings Form"), DescriptionAttribute("Delete temporary dump files")]
        public bool AutoDeleteResultsFiles { get; set; }

        [CategoryAttribute("UMDH Tracer Settings Form"), DescriptionAttribute("Executable name of the process to dump")]
        public string ExecutableName { get; set; }

        [CategoryAttribute("UMDH Tracer Settings Form"), DescriptionAttribute("Period in minutes between dumps")]
        public int Period { get; set; }

        [CategoryAttribute("UMDH Tracer Settings Form"), DescriptionAttribute("Number of dump")]
        public int DumpCount { get; set; }

        [CategoryAttribute("UMDH Tracer Settings Form"), DescriptionAttribute("UMDH Timeout in Minutes (before considering it hanged)")]
        public int UmdhTimeout { get; set; }

        public Settings()
        {
            UmdhPath = @"c:\program files\debugging tools for windows (x86)\umdh.exe";
            AutoDeleteResultsFiles = false;
            ExecutableName = "datacaptoris.exe";
            Period = 5;
            DumpCount = int.MaxValue;
            UmdhTimeout = 5;
        }

        static public string AssemblyDirectory
        {
            get
            {
                string codeBase = Assembly.GetExecutingAssembly().CodeBase;
                UriBuilder uri = new UriBuilder(codeBase);
                string path = Uri.UnescapeDataString(uri.Path);
                return Path.GetDirectoryName(path);
            }
        }

        static public string SettingsPath { get { return Path.Combine(AssemblyDirectory, "settings.xml"); } }

        static public bool Load(out Settings settings)
        {
            try
            {
                XmlSerializer serializerObj = new XmlSerializer(typeof(Settings));
                FileStream readFileStream = new FileStream(SettingsPath, FileMode.Open, FileAccess.Read, FileShare.Read);
                settings = (Settings)serializerObj.Deserialize(readFileStream);
                readFileStream.Close();
                return true;
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Settings Load Catch Occur " + ex.Message);
                settings = new Settings();
                return false;
            }
        }

        public bool Save()
        {
            try
            {
                XmlSerializer serializerObj = new XmlSerializer(typeof(Settings));
                TextWriter writeFileStream = new StreamWriter(SettingsPath);
                serializerObj.Serialize(writeFileStream, this);
                writeFileStream.Close();
                return true;
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Settings Save Catch Occur " + ex.Message);
                return false;
            }
        }

        #region Implementation of ICloneable
        public Settings Clone()
        {
            return new Settings
                       {
                           UmdhPath = UmdhPath,
                           AutoDeleteResultsFiles = AutoDeleteResultsFiles,
                           ExecutableName = ExecutableName,
                           Period = Period,
                           DumpCount = DumpCount,
                           UmdhTimeout = UmdhTimeout
                       };
        }
        #endregion
    }



}
